﻿#include "GraphicsWithDx9.h"
#include <array>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

GraphicsWithDx9::GraphicsWithDx9(HWND hwnd)
{
    this->_viewRect = new RECT;
    GetWindowRect(hwnd, this->_viewRect);
    this->_viewWidth = this->_viewRect->right - this->_viewRect->left;
    this->_viewHeight = this->_viewRect->bottom - this->_viewRect->top;
    // 创建d3d对象
    this->_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
    // 检测设备性能
    D3DCAPS9 caps;
    this->_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
    DWORD vp = 0;

    if (caps.DevCaps & D3DDEVCAPS_HWRASTERIZATION)
    {
        vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;

    } else
    {
        vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    }
    // 填充parmeter
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp, sizeof(d3dpp));

    d3dpp.AutoDepthStencilFormat     = D3DFMT_D24S8;
    d3dpp.BackBufferCount             = 1;
    d3dpp.BackBufferFormat             = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferHeight             = this->_viewHeight;
    d3dpp.BackBufferWidth             = this->_viewWidth;
    d3dpp.EnableAutoDepthStencil     = TRUE;
    d3dpp.Flags                         = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.hDeviceWindow                 = hwnd;
    d3dpp.MultiSampleQuality         = 0;
    d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
    d3dpp.PresentationInterval         = D3DPRESENT_INTERVAL_IMMEDIATE;
    d3dpp.SwapEffect                 = D3DSWAPEFFECT_DISCARD;
    d3dpp.Windowed                     = TRUE;

    this->_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, vp, &d3dpp, &this->_pDevice);
    D3DXCreateFontW(this->_pDevice, this->_viewHeight/3, 5, 0, D3DX_DEFAULT, FALSE, DEFAULT_CHARSET, 0, 0, 0, L"Times New Roman", &this->_pFont);
    // 构建插槽
    this->_slotsNum = 4;
    this->_slots = new std::vector<char*>(this->_slotsNum);
    this->_slots->resize(this->_slotsNum, nullptr);
}

GraphicsWithDx9::~GraphicsWithDx9()
{
    if(this->_pDevice != nullptr)
    {
        this->_pDevice->Release();
    }
    if(this->_pD3D != nullptr)
    {
        this->_pD3D->Release();
    }
    if(this->_pFont != nullptr)
    {
        this->_pFont->Release();
    }
    delete this->_viewRect;
    delete this->_slots;
}

void GraphicsWithDx9::doFrame() const
{
    this->_pDevice->Clear(0, NULL, D3DCLEAR_STENCIL|D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    this->_pDevice->BeginScene();
    this->drawSlots();
    this->_pDevice->EndScene();
    
    this->_pDevice->Present(NULL, NULL, NULL, NULL);
    
}

void GraphicsWithDx9::putSlot(char* text, int index) const
{
    auto iter = this->_slots->begin() + index;
    *iter = text;
}

void GraphicsWithDx9::drawSlots() const
{
    RECT rec = {};
    rec.left = 0;
    rec.top = 0;
    rec.right = 0;
    rec.bottom = 0;
    int row = 0;
    int col = 0;
    const char* text = "text";
    rec.right = 0;
    rec.bottom = 0;
    for(int i = 0; i < this->_slotsNum; i++)
    {
        if(this->_slots->at(i) == nullptr)
        {
            continue;
        }
        row = i/2;
        col = i%2;
        rec.left = row*(this->_viewWidth/2);
        rec.top = col*(this->_viewHeight/2);
        rec.right = rec.left + this->_viewWidth/2;
        rec.bottom = rec.top + this->_viewHeight/2;
        this->_pFont->DrawTextA(NULL, this->_slots->at(i), -1, &rec, DT_LEFT|DT_VCENTER, D3DCOLOR_XRGB(24, 24, 22));
    }
}
